const express = require('express');
// 引入暴露的模块
const pool = require('../mysql.js');

// 使用路由方法
const route = express.Router();


// 查询所有数据
route.get('/clock',(req,res,next)=>{
    pool.query('select * from user_clock where kid=?',[req.query.kid], (err, result) => {

        if (err) {
            next(err);
            return
        }
        console.log(result)

        if (result.length) {
            res.send({ code: 200, msg: '查找成功',data:result})
        } else {
            res.send({ code: 201, msg: '暂无数据'})
        }

    }) 
})
// 保存自定义打卡内容
route.post('/zdyclock',(req,res,next)=>{
    pool.query('insert into clock_zdy set?',[req.body], (err, result) => {
        console.log(req.body)
        if (err) {
            next(err)
            return
        }
        res.send({ code: 200, msg: '添加成功' })
    }) 
})
// 删除自定义打卡
route.delete('/deleteczdy',(req,res)=>{
    pool.query('delete from clock_zdy where zid=? and idx=?',[req.query.zid,req.query.idx],(err,result) => {
        if (err) {
            next(err)
            return
        }
        if (result.affectedRows) {
            res.send({ code: 200, msg: '删除成功' })
        } else {
            res.send({ code: 201, msg: '数据不存在,删除失败' })
        }

    })
})


// 查询自定义内容
route.get('/zdyinfo',(req,res,next)=>{
    console.log(11)
    pool.query('select * from clock_zdy where zid=?',[req.query.zid], (err, result) => {

        if (err) {
            next(err);
            return
        }
        console.log(result)

        if (result.length) {
            res.send({ code: 200, msg: '查找成功',data:result})
        } else {
            res.send({ code: 201, msg: '暂无数据'})
        }

    }) 
})

// 打卡成功传入内容
route.post('/updataclock',(req,res,next)=>{

     
    pool.query('insert into user_clock set?',[req.body], (err, result) => {
        console.log(req.body)

        if (err) {
            next(err)
            return
        }
        res.send({ code: 200, msg: '打卡成功' })

    }) 
  
})

module.exports = route;